package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.AppType;
import com.ygqh.baby.ao.FileType;
import com.ygqh.baby.ao.QrCodeType;
import com.ygqh.baby.service.AbstractService;
import com.ygqh.baby.service.FileService;
import com.ygqh.baby.service.WxSmallAuthorityService;
import com.ygqh.baby.service.YgQrCodeService;
import com.ygqh.baby.utils.ImageUtils;
import com.ygqh.baby.utils.QRCodeUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class YgQrCodeServiceImpl extends AbstractService implements YgQrCodeService {
    protected Logger logger = Logger.getLogger(this.getClass());

    @Value("${promotion.link}")
    protected String promotionLink;
    @Value("${image.domain}")
    private String domain;

    @Autowired
    private FileService fileService;
    @Autowired
    private WxSmallAuthorityService wxSmallAuthorityService;

    @Override
    public File createQrCodeFile(String url, QrCodeType qrCodeType, String fileName) throws Exception {

        String path = getContentPath();
        String realPath = path + fileName;

        File file = QRCodeUtil.getFile(url, realPath, fileName);

        return file;
    }

    @Override
    public String uploadQrCodeToAliYun(File file) throws Exception {
        return fileService.uploadImage(file, FileType.QrCode, true);

    }

    @Override
    public String getQrCodeUrl(String url) throws Exception {
        File qrCodeFile = createQrCodeFile(url, QrCodeType.Default, System.currentTimeMillis() + ".jpg");
        return uploadQrCodeToAliYun(qrCodeFile);
    }

    @Override
    public String getQrCodeForInvite(AppType appType, String inviteCode, String url) throws Exception {
        String qrcode;

        if (AppType.Small.equals(appType)) {
            qrcode = wxSmallAuthorityService.getSamllQrcode(url, 1);
        } else {
            String fileName = QrCodeType.UserAgentInvite + "_" + inviteCode + ".jpg";
            File file = createQrCodeFile(url, QrCodeType.UserAgentInvite, fileName);
            qrcode = file.getPath();
        }
        // 底图阿里云路径
        String sourceUrl = domain + "default/coupon_ditu.jpg";
        // 本地路径
        String targetDir = getContentPath() + "/" + QrCodeType.UserAgentInvite;
        // 本地路径
        String targetFileName = QrCodeType.UserAgentInvite + "_ditu.jpg";
        File ditu = ImageUtils.getFile(targetDir + "/" + targetFileName);

        if (!ditu.isFile() && !ditu.exists()) {
            // 将底图下载的本地
            ImageUtils.saveImageToDisk(sourceUrl, targetDir, targetFileName);
        }

        // 合并二维码与底图
        String srcImageFile = targetDir + "/" + targetFileName;
        String destImageFile = targetDir + "/" + inviteCode + ".jpg";
        ImageUtils.pressImageMark2(qrcode, srcImageFile, destImageFile, 148, 190, 1.0f, 200, 200);
        return uploadQrCodeToAliYun(new File(destImageFile));
    }
}
